//
//  TimelineCacheManager.m
//  weibo3
//
//  Created by 吴天 on 11-5-25.
//  Copyright 2011年 nfsysu. All rights reserved.
//

#import "TimelineCacheManager.h"
#import "Status.h"
#import "WComment.h"


@implementation TimelineCacheManager

@synthesize followingStatusCache, mentionsStatusCache, commentStatusCache, selfStatusCache, firstFollowingCall, firstCommentCall, firstMentionsCall, firstSelfCall, lastSelfStatusID, lastCommentStatusID, lastMentionStatusID, lastFollowingStatusID, endSelfStatusID, endCommentStatusID, endMentionStatusID, endFollowingStatusID;

- (id)init
{
    self = [super init];
    if (self) {
        // Following cache
		followingStatusCache = [NSMutableArray array];
		firstFollowingCall = YES;
		lastFollowingStatusID = [NSString string];
        endFollowingStatusID = [NSString string];
        
        // Mentions cache
		mentionsStatusCache = [NSMutableArray array];
		firstMentionsCall = YES;
		lastMentionStatusID = [NSString string];
        endMentionStatusID = [NSString string];
        
        // Comments cache
		commentStatusCache = [NSMutableArray array];
		firstCommentCall = YES;
		lastCommentStatusID = [NSString string];
        endCommentStatusID = [NSString string];
        
        // Self cache
		selfStatusCache = [NSMutableArray array];
		firstSelfCall = YES;
		lastSelfStatusID = [NSString string];
        endSelfStatusID = [NSString string];
    }
    
    return self;
}

- (void) resetAllCaches {
	[commentStatusCache removeAllObjects];
	[followingStatusCache removeAllObjects];
	[mentionsStatusCache removeAllObjects];
	[selfStatusCache removeAllObjects];
	
	firstCommentCall = YES;
	firstFollowingCall = YES;
	firstMentionsCall = YES;
	firstSelfCall = YES;
}

- (NSMutableArray *) setStatusesForTimelineCache:(NSUInteger)timelineCacheType 
                                withNotification:(NSNotification *)note{
	BOOL *firstCall;
	NSMutableArray *cache;
	NSString **lastStatusID;
	if (timelineCacheType == FollowingTimelineCacheType) {
		firstCall = &firstFollowingCall;
		cache = followingStatusCache;
		lastStatusID = &lastFollowingStatusID;
	} else if (timelineCacheType == MentionsTimelineCacheType) {
		firstCall = &firstMentionsCall;
		cache = mentionsStatusCache;
		lastStatusID = &lastMentionStatusID;
	} else if (timelineCacheType == CommentTimelineCacheType) {
		firstCall = &firstCommentCall;
		cache = commentStatusCache;
		lastStatusID = &lastCommentStatusID;
	} else if (timelineCacheType == SelfTimelineCacheType) {
		firstCall = &firstSelfCall;
		cache = selfStatusCache;
		lastStatusID = &lastSelfStatusID;
	}
	
    
	if (*firstCall) {
		[cache setArray:(NSArray *)[note object]];
	} else {
		NSIndexSet *indexSet = [NSIndexSet 
                                indexSetWithIndexesInRange:NSMakeRange(0, 
                                                                       [(NSArray *)[note object] count])];
		[cache insertObjects:(NSArray *)[note object] atIndexes:indexSet];
	}
    if ([cache count] > 0) {
        if (timelineCacheType == CommentTimelineCacheType) {
            WComment * lastComment = (WComment *)[cache objectAtIndex:0];
            *lastStatusID = [NSString stringWithFormat:@"%lld",[lastComment aid]];
        }
        else{
            
            Status * lastStatus = (Status *)[cache objectAtIndex:0];
            *lastStatusID = [NSString stringWithFormat:@"%lld",[lastStatus aid]];
        }
        *firstCall = NO;
	}
	return cache;
}

- (NSMutableArray *) setStatusesForTimelineCache:(NSUInteger)timelineCacheType
                                       withArray:(NSArray *)array{
    
    BOOL *firstCall;
	NSMutableArray *cache;
	NSString **lastStatusID;
    NSString **endStatusID;
    
	if (timelineCacheType == FollowingTimelineCacheType) {
		firstCall = &firstFollowingCall;
		cache = followingStatusCache;
		lastStatusID = &lastFollowingStatusID;
        endStatusID = &endFollowingStatusID;
	} else if (timelineCacheType == MentionsTimelineCacheType) {
		firstCall = &firstMentionsCall;
		cache = mentionsStatusCache;
		lastStatusID = &lastMentionStatusID;
        endStatusID = &endMentionStatusID;
	} else if (timelineCacheType == CommentTimelineCacheType) {
		firstCall = &firstCommentCall;
		cache = commentStatusCache;
		lastStatusID = &lastCommentStatusID;
        endStatusID = &endCommentStatusID;
	} else if (timelineCacheType == SelfTimelineCacheType) {
		firstCall = &firstSelfCall;
		cache = selfStatusCache;
		lastStatusID = &lastSelfStatusID;
        endStatusID = &endSelfStatusID;
	}
	
    
	if (*firstCall) {
		[cache setArray:[NSMutableArray arrayWithArray:array]];
	} else {
		NSIndexSet *indexSet = [NSIndexSet 
                                indexSetWithIndexesInRange:NSMakeRange(0, 
                                                                       [array count])];
		[cache insertObjects:array atIndexes:indexSet];
	}
    if ([cache count] > 0) {
        if (timelineCacheType == CommentTimelineCacheType) {
            WComment * lastComment = (WComment *)[cache objectAtIndex:0];
            *lastStatusID = [NSString stringWithFormat:@"%lld",[lastComment aid]];
            WComment * endComment = (WComment *)[cache lastObject];
            *endStatusID = [NSString stringWithFormat:@"%lld",[endComment aid]];
        }
        else{
            
            Status * lastStatus = (Status *)[cache objectAtIndex:0];
            *lastStatusID = [NSString stringWithFormat:@"%lld",[lastStatus aid]];
            Status * endStatus = (Status *)[cache lastObject];
            *endStatusID = [NSString stringWithFormat:@"%lld",[endStatus aid]];
        }
        *firstCall = NO;
	}
	return cache;
}

- (NSMutableArray *) appendStatusesForTimelineCache:(NSUInteger)timelineCacheType
                                          withArray:(NSArray *)array{
    
	NSMutableArray *cache;
	NSString **lastStatusID;
    NSString **endStatusID;
    
	if (timelineCacheType == FollowingTimelineCacheType) {
		cache = followingStatusCache;
		lastStatusID = &lastFollowingStatusID;
        endStatusID = &endFollowingStatusID;
	} else if (timelineCacheType == MentionsTimelineCacheType) {
		cache = mentionsStatusCache;
		lastStatusID = &lastMentionStatusID;
        endStatusID = &endMentionStatusID;
	} else if (timelineCacheType == CommentTimelineCacheType) {
		cache = commentStatusCache;
		lastStatusID = &lastCommentStatusID;
        endStatusID = &endCommentStatusID;
	} else if (timelineCacheType == SelfTimelineCacheType) {
		cache = selfStatusCache;
		lastStatusID = &lastSelfStatusID;
        endStatusID = &endSelfStatusID;
	}
    
	[cache addObjectsFromArray:array];    
    if ([cache count] > 0) {
        if (timelineCacheType == CommentTimelineCacheType) {
            WComment * lastComment = (WComment *)[cache objectAtIndex:0];
            *lastStatusID = [NSString stringWithFormat:@"%lld",[lastComment aid]];
            WComment * endComment = (WComment *)[cache lastObject];
            *endStatusID = [NSString stringWithFormat:@"%lld",[endComment aid]];
        }
        else{
            
            Status * lastStatus = (Status *)[cache objectAtIndex:0];
            *lastStatusID = [NSString stringWithFormat:@"%lld",[lastStatus aid]];
            Status * endStatus = (Status *)[cache lastObject];
            *endStatusID = [NSString stringWithFormat:@"%lld",[endStatus aid]];
        }
	}
	return cache;
    
}


- (void)dealloc
{
    [super dealloc];
}

@end
